home *** CD-ROM | disk | FTP | other *** search
- Path: news.bridge.net!news
- From: David Byrden <100101.2547@compuserve.com>
- Newsgroups: comp.lang.c++
- Subject: Re: RE: Copt constructing an already default ....
- Date: 26 Jan 1996 14:22:32 GMT
- Organization: self-employed
- Message-ID: <4eao38$ab7@news.bridge.net>
- References: <4e9dfr$spc@dub-news-svc-6.compuserve.com>
- NNTP-Posting-Host: ppp-mia1-49.bridge.net
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.1N (Windows; I; 16bit)
-
-
- The lack of knowledge of good C++ practice displayed in the previous
- post, is frightening.
-
- >>> I've go an object which has already been constructed via its default
- >>> constructor which just sets all pointers to NULL. What's the best
- >>> way to deep-copy into it ??
-
- Firstly, the concept of shallow versus deep copying is not a solid
- concept. Any given class may adopt a scheme for representing its data
- which would make shallow copying illegal and deep copying hard to define.
-
- If you make a default-constructed object X, and you then want to make it
- a copy of another object Y, you ought to be able to do so like this;
-
-
- Thing X;
- X = Y ;
-
- in other words, assignment should have the same END result as
- constructing X from Y. You may choose to design a class where it does
- not, but then you are making up your own rules for it, so I can't tell
- you what is best.
-
-
- >> A & operator= (const A & a ) { deepCopy( a ); return *this; }
-
- Given the version of deepcopy() which is shown in the previous post, this
- code will result in a memory leak almost every time an object of class A
- is copied. The old memory is not deleted.
-
-
- >> It is not a good idea to memcpy() to an object. If the object has no
- >> virtual functions you will usually be OK
-
- If the object has any pointers to internal parts of itself, if it
- maintains any kind of reference or usage count, or if the copy
- constructor or destructor or assignment operator have any other side
- effects, or if it has base classes encapsulating their own (unknown,
- changeable) behaviour, you will not be OK.
-
-
- >> but if the class does have virtual functions, you will most likely
- >> over-write the virtual function table pointer and completely hose
- >> yourself.
-
- As a matter of fact, overwriting the virtual function table pointer is
- one of the few things that will NOT "hose" you if you use memcpy() to
- copy objects of the same class.
-
-
- David
-
-
-
-